﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.ComponentModel.DataAnnotations;
using Helpers;

namespace DataModel
{
    public class DataLayer : DbContext
    {
        static DataLayer _data;
        public static DataLayer Data
        {
            get
            {
                if (_data == null)
                {
                    //Database.SetInitializer<DataLayer>(new DropCreateDatabaseAlways<DataLayer>());
                    _data = new DataLayer();
                }
                return _data;
            }
        }

        public DbSet<Settings> Settings { get; set; }
        public DbSet<Author> Authors { get; set; }
        public DbSet<Publisher> Publishers { get; set; }
        public DbSet<Book> Books { get; set; }
        public DbSet<BookCopy> BookCopies { get; set; }

        public DbSet<Phone> Phones { get; set; }
        public DbSet<Address> Addresses { get; set; }
        public DbSet<Person> Persons { get; set; }

        public string CreateDatabaseScript()
        {
            return ((IObjectContextAdapter)this).ObjectContext.CreateDatabaseScript();
        }

        public DataLayer()
            : base("xlibris")
        {
            try
            {
                //string s = new System.Data.EntityClient.EntityConnectionStringBuilder
                //{
                //    Provider = "FirebirdSql.Data.FirebirdClient",
                //    ProviderConnectionString = new FirebirdSql.Data.FirebirdClient.FbConnectionStringBuilder
                //    {
                //        UserID = "SYSDBA",
                //        Password = "masterkey",
                //        DataSource = "localhost",
                //        Database = @"FirebirdSql.Data.FirebirdClient",
                //        Port = 3050,
                //        Dialect = 3,
                //        Charset = "UTF_8",
                //        Pooling = true,
                //        ConnectionLifeTime = 5                    
                //    }.ConnectionString
                //}.ConnectionString;
                //Database.Connection.ConnectionString = @"User=SYSDBA;Password=masterkey;Database=C;DataSource=localhost;Port=3050;Dialect=3;Charset=iso8859_1;Role=;Connection lifetime=15;Pooling=true;MinPoolSize=0;MaxPoolSize=50;Packet Size=8192;ServerType=0";
            }
            catch (Exception ex)
            {
                Logger.Write("Exception " + ex.Message);
            }
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            try
            {
                base.OnModelCreating(modelBuilder);

                modelBuilder.Configurations.Add(new BookConfiguration());
                modelBuilder.Configurations.Add(new PersonConfiguration());
                modelBuilder.Configurations.Add(new PhoneConfiguration());
                modelBuilder.Configurations.Add(new PublisherConfiguration());
                modelBuilder.Configurations.Add(new BookCopyConfiguration());
                modelBuilder.Configurations.Add(new AuthorConfiguration());
                modelBuilder.Configurations.Add(new AddressConfiguration());
                modelBuilder.Configurations.Add(new StudentConfiguration());
                modelBuilder.Configurations.Add(new TeacherConfiguration());
                modelBuilder.Configurations.Add(new SettingsConfiguration());
            }
            catch (Exception ex)
            {
                Logger.Write("OnModelCreating Exception " + ex.Message);
            }
        }
    }
}
